﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace FmcWebsite.Web
{
    public class Rich2Html
    {
        static public string Convert(string rich)
        {
            var c = new Rich2Html(rich);
            return c.GetHtml();
        }

        string rich;

        public Rich2Html(string richtext)
        {
            this.rich = richtext;
        }

        private string ConvertRichTag(string tag, int par)
        {
            if (tag == "b")
            {
                if (par == 0)
                    return "</b>";
                else
                    return "<b>";
            }

            if (tag == "i")
            {
                if (par == 0)
                    return "</i>";
                else
                    return "<i>";
            }

            if (tag == "ul")
                return "<u>";

            if (tag == "ulnone")
                return "</u>";

            if (tag == "par")
            {
                return "<br/>";
            }

            if ((tag == "{") || (tag == "}"))
            {
                byte b = (byte)tag[0];
                return "&#" + b.ToString() + ";";
            }

            if ((tag.Length == 1) && (par < 0))
            {                                               
                return tag;
            }

            return "";
        }

        private void ReadRichTag(ref string rich, out string tag, out int par)
        {            

            if ((rich.Length > 1) && (rich[0] == '\\'))
            {
                rich = rich.Remove(0, 1); // remove the slash

                // reading the tag

                // read the first character
                tag = rich[0].ToString();
                rich = rich.Remove(0, 1);

                if (Char.IsLetter(tag[0]))
                {
                    // loop until end of tag
                    while ((rich.Length > 0) &&
                           Char.IsLetter(rich[0]))
                    {

                        tag += rich[0];
                        rich = rich.Remove(0, 1);
                    }
                }

                tag = tag.Trim();

                // reading parameter
                par = -1;
                while ((rich.Length > 0) && Char.IsDigit(rich[0]))
                {
                    if (par < 0)
                        par = 0;
                    par *= 10 + int.Parse(rich[0].ToString());
                    rich = rich.Remove(0, 1);
                }

                if ((rich.Length > 0) && Char.IsWhiteSpace(rich[0]))
                    rich = rich.Substring(1);
            }
            else
                throw new Exception("Rich tag is empty or not valid.");
        }

        private string GetContentOnly(string raw)
        {
            if (!raw.Contains('{'))
                return raw;

            string result = "";
            int level = 0;
            foreach (Char c in raw)
            {
                switch (c)
                {
                    case '{':
                        level++;
                        break;
                    case '}':
                        level--;
                        break;
                    default:
                        if (level == 1)
                            result += c;
                        break;
                }                
            }
            return result.Trim();
        }

        public string GetHtml()
        {            
            string html = "";

            string[] lines = rich.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string line in lines)
            {
                string text = line;
                while (text.Length > 0)
                {
                    if (text[0] == '\\')
                    {
                        string tag = "";
                        int par = 0;
                        ReadRichTag(ref text, out tag, out par);
                        html += ConvertRichTag(tag, par);
                    }
                    else
                    {
                        int i = text.IndexOf('\\');
                        if (i > -1)
                        {
                            html += HttpUtility.HtmlEncode(text.Substring(0, i));
                            text = text.Remove(0, i);
                        }
                        else
                        {
                            html += HttpUtility.HtmlEncode(text);
                            text = "";
                        }
                    }
                }
                html += "\r\n";
            }
            return GetContentOnly(html); 
        }
    }
}
